
import { EM, EEventName } from './Manager/EventManager';
/*
 * @features: 战斗模块
 * @description: 战斗模块的流程控制
 * @Date: 2020-08-12 22:56:44
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2020-09-01 21:10:29
 * @LastEditors: judu233
 */

import CampBase, { ICampDataType } from "./Camp/CampBase";
import CardBase, { ICardDataType } from "./Card/CardBase";
import FightManager from "./Manager/FightManager";
import CampManager, { ECamp } from "./Manager/CampManager";
import SkillManager from "./Manager/SkillManager";

const { ccclass, property } = cc._decorator;

/**1个阵营的所有数据格式 卡牌加阵营 */
export interface IOneCampDataType {
    /**存储阵营的数据 */
    campData: ICampDataType,
    cardData: ICardDataType[],
}

/**  
    * @Title : 战斗流程模块控制
    * @Description : 函数说明
 **/
@ccclass
export default class FightProcess extends cc.Component {
    /**阵营管理 */
    @property(CampManager)
    public campManager: CampManager = null;

    /**技能管理 */
    @property(SkillManager)
    public skillManager: SkillManager = null;

    /**战斗流程是否是自动的 */
    public isAuto = false;

    /**敌人··电脑是否轮流攻击 */
    public isComputerTurnAttack: boolean = true;

    /**玩家是否轮流攻击 */
    public isPlayerTurnAttack: boolean = true;

    onLoad() {
        //初始化保存引用
        FightManager.process = this;
        FightManager.campManager = this.campManager;
        FightManager.skillManager = this.skillManager;
        let data = <IOneCampDataType>{
            campData: {
                camp: ECamp.Player,
            },
            cardData: [
                {
                    hp: 100,
                    hpUp: 1,
                    camp: ECamp.Player,
                    id: `aaa`,
                    name: `player1`,
                    death: false,
                },
                {
                    hp: 200,
                    hpUp: 2,
                    camp: ECamp.Player,
                    id: `bbb`,
                    name: `player2`,
                    death: false,
                },
                {
                    hp: 300,
                    hpUp: 3,
                    camp: ECamp.Player,
                    id: `ccc`,
                    name: `player3`,
                    death: false,
                },
            ]
        }

        let data2 = <IOneCampDataType>{
            campData: {
                camp: ECamp.Computer,
            },
            cardData: [
                {
                    hp: 100,
                    hpUp: 1,
                    camp: ECamp.Computer,
                    id: `aaa`,
                    name: `Computer1`,
                    death: false,
                },
                {
                    hp: 200,
                    hpUp: 2,
                    camp: ECamp.Computer,
                    id: `bbb`,
                    name: `Computer2`,
                    death: false,
                },
                {
                    hp: 300,
                    hpUp: 3,
                    camp: ECamp.Computer,
                    id: `ccc`,
                    name: `Computer3`,
                    death: false,
                },
            ]
        }
        this.campManager.initAllCamp([data, data2]);
    }

    start() {
        this.startProcess();
    }

    btnAttack() {
        EM.fire(EEventName.PlayerAttack, `attack-btn`);
    }
    btnSkil() {
        EM.fire(EEventName.PlayerSkill, `skill-btn`);
    }

    /**关于流程的操作 */
    /**开始流程 */
    public startProcess() {
        cc.log(`**********【startGame】***********`);
        EM.fire(EEventName.FightStart);
        //开始流程
        let proocess = this.nextProcess();
        proocess.then((winCamp) => {
            this.overProcess(<ECamp>winCamp);
        }).catch((err) => {
            cc.error(`[循环流程逻辑出错]:${err}`);
        });
    }

    /**流程回合操作 */
    private async nextProcess() {
        let winCamp: ECamp, isDeath: boolean;
        //开始流程
        while (true) {
            cc.log(`------------【next】-----------`);
            //开始回合
            //随机先手阵营
            let firstCamp = this.campManager.getFirstCamp();
            let lastCamp = this.campManager.getLastCamp();
            EM.fire(EEventName.RoundStart, { first: firstCamp, last: lastCamp });

            //先手阵营开始攻击
            ({ winCamp, isDeath } = await this.readyAttackCamp(firstCamp, lastCamp));
            if (isDeath) break;

            //后手阵营开始攻击
            ({ winCamp, isDeath } = await this.readyAttackCamp(lastCamp, firstCamp));
            if (isDeath) break;
            cc.log(`-----------【nextOver】------------`);
            EM.fire(EEventName.RoundOver);
        }
        return winCamp;
    }

    /**攻击 */
    private async readyAttackCamp(firstCamp: CampBase, lastCamp: CampBase) {
        //先手阵营开始攻击
        cc.log(`阵营[${firstCamp.camp}]开始准备攻击`);
        let card = firstCamp.getSurviveCardForRandom(1)[0];
        if (card) {
            await card.attackCamp(lastCamp);
            //检查是否胜利
            cc.log(`阵营[${firstCamp.camp}]攻击完成`);
        }
        return this.checkWin(firstCamp, lastCamp);
    }

    /**检查是否获胜 */
    public checkWin(firstCamp: CampBase, lastCamp: CampBase) {
        let checkIfon = { winCamp: firstCamp.camp, isDeath: false };
        if (lastCamp.checkCardAllDeath()) checkIfon.isDeath = true;
        return checkIfon;
    }

    /**结束操作 */
    public overProcess(winCamp: ECamp) {
        cc.log(`********【GameOver:获胜方${winCamp}】***********`);
        EM.fire(EEventName.FightOver, winCamp);
    }

}
